<?php
/**
 * 每周三 浏览榜单用户每周触达
 * 为进一步提高榜单访问uv及留存，计划在每周三榜单更新时触达老用户，告知榜单已更新，引导点击查看
 * 更新时间：每周三10:00
 */
ini_set('display_errors', 1);
ini_set('memory_limit', -1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\bigData\RankingHotUser;
use Biz\model\wiki\WikiRankingHot as mWikiRankingHot;
use Biz\service\wiki\WikiBasic;
use Biz\service\wikiRanking\Ranking;
use Doraemon\pockets\constInterface\Push\PushId;
use Doraemon\service\Push\PushService;

//浏览榜单用户每周触达
$list = [];
$page = 1;
do {
    $uid = null;
    $list = RankingHotUser::getUserRanking($page, 200, $uid);
    $rankingIds = array_values($list);
    $rankingDay = Ranking::getRankingDay();
    $where = [
        'ranking_id' => $rankingIds,
        'ranking_state' => mWikiRankingHot::RANKING_STATE_SHOW,
        'ranking_day' => $rankingDay,
        'ORDER' => ['top_num' => 'ASC', 'score' => 'DESC']
    ];
    $hotList = mWikiRankingHot::selectByWhere($where);
    $basicIds = $hotList ? array_column($hotList, 'basic_id') : [];
    $basicIds = array_unique(array_filter($basicIds));
    $basicList = $basicIds ? WikiBasic::getWikiBasicInfos($basicIds) : [];
    $hotRankingList = [];
    if ($hotList && $basicList) {
        foreach ($hotList as $row) {
            $basicInfo = isset($basicList[$row['basic_id']]) ? $basicList[$row['basic_id']] : [];
            if ($basicInfo) {
                $row['basic_title'] = $basicInfo['title'];
                $hotRankingList[$row['ranking_id']][] = $row;
            }
        }
        foreach ($list as $uid => $rankingId) {
            if (isset($hotRankingList[$rankingId])) {
                $link = Ranking::getRankingUrl($rankingId, 1);
                $rankingName = '';
                $goodsNames = [];
                foreach ($hotRankingList[$rankingId] as $row) {
                    $rankingName = $row['ranking_name'];
                    $goodsNames[] = $row['basic_title'];
                }
                var_dump('uid:' . $uid . '#ranking_name:' . $rankingName . '#top count:' . count($goodsNames));
                pushMsg($uid, $rankingName, $goodsNames, $link);
            }
        }
    }
    $page++;
} while ($list);

function pushMsg($uid, $rankingName, $goodsNames, $link)
{
    if ($uid) {
        $title = '本周【' . $rankingName . '】榜单已更新';
        $desc = '快来看看住友正在热议的' . $rankingName . ' TOP ' . count($goodsNames) . ' 吧！';
        $pushId = PushId::BRAND_RANK_WIKI_ACTION . 'ranking_hot_user_' . date('Y-m-d');
        $params = [
            'uid' => $uid,
            'title' => $title,
            'alert' => $desc,
            'param' => [
                'link' => $link,
                'push_id' => $pushId,
                'image' => ''
            ]
        ];
        PushService::send($params);

        $data = [
            'msgCode' => 0,
            'to_uid' => $uid,
            'msgParams' => [
                'title' => $title,
                'detail' => $desc,
                'img' => 'https://img.haohaozhu.cn/Op-imageShow/96b1230ku0af0lz00rqs9cb?iv=1',
                'link' => $link,
                'type' => 2,
            ]
        ];
//        \Doraemon\service\Sysmsg::addMsg(0, $uid, $data['msgParams']);
        \Doraemon\cache\Queue::insertQueue('queue/Sysmsg/Send', $data, \Doraemon\cache\Queue::QUEUE_DORAEMON);
    }
}